package cn.edu.sdjzu.knoverse.repository;

import cn.edu.sdjzu.knoverse.model.entity.kg.ComponentNode;
import cn.edu.sdjzu.knoverse.model.entity.kg.KnowledgeNode;
import cn.edu.sdjzu.knoverse.model.entity.kg.FileNode;
import cn.edu.sdjzu.knoverse.model.entity.kg.CaseNode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

@Repository
public interface ComponentRepository extends Neo4jRepository<ComponentNode, Long> {

    /**
     * 根据案例ID查找组件
     * @param caseId 案例ID
     * @return 该案例下的所有组件
     */
    @Query("MATCH (c:Case)-[:CASE_COMPONENT]->(cmp:Component) " +
           "WHERE c.nodeId = $caseId " +
           "RETURN cmp")
    Set<ComponentNode> findByCaseId(Long caseId);
    
    /**
     * 创建案例与组件的关系
     * @param caseId 案例ID
     * @param componentId 组件ID
     * @param name 关系名称
     * @param description 关系描述
     */
    @Query("MATCH (c:Case) WHERE c.nodeId = $caseId " +
           "MATCH (cmp:Component) WHERE cmp.nodeId = $componentId " +
           "MERGE (c)-[r:CASE_COMPONENT {name: $name, description: $description}]->(cmp)")
    void createCaseComponentRelationship(Long caseId, Long componentId, String name, String description);
    
    /**
     * 创建组件与文件的关系
     * @param componentId 组件ID
     * @param fileId 文件ID
     * @param name 关系名称
     * @param description 关系描述
     */
    @Query("MATCH (cmp:Component) WHERE cmp.nodeId = $componentId " +
           "MATCH (f:File) WHERE f.fileId = $fileId " +
           "MERGE (cmp)-[r:COMPONENT_FILE {name: $name, description: $description}]->(f)")
    void createComponentFileRelationship(Long componentId, Long fileId, String name, String description);
    
    /**
     * 删除组件的所有文件关系
     * @param componentId 组件ID
     */
    @Query("MATCH (cmp:Component)-[r:COMPONENT_FILE]->(f:File) WHERE cmp.nodeId = $componentId DELETE r")
    void deleteComponentFileRelationships(Long componentId);

    /**
     * 创建组件与知识点的关联关系
     * @param componentId 组件ID
     * @param knowledgeId 知识点ID
     * @param name 关系名称
     * @param description 关系描述
     */
    @Query("MATCH (cmp:Component) WHERE cmp.nodeId = $componentId " +
           "MATCH (k:Knowledge) WHERE k.nodeId = $knowledgeId " +
           "MERGE (cmp)-[r:RELATED {name: $name, description: $description}]->(k)")
    void createComponentKnowledgeRelationship(Long componentId, Long knowledgeId, String name, String description);
    
    /**
     * 删除组件的所有知识点关联关系
     * @param componentId 组件ID
     */
    @Query("MATCH (cmp:Component)-[r:RELATED]->(k:Knowledge) WHERE cmp.nodeId = $componentId DELETE r")
    void deleteComponentKnowledgeRelationships(Long componentId);
    
    /**
     * 获取组件关联的所有知识点
     * @param componentId 组件ID
     * @return 知识点列表
     */
    @Query("MATCH (cmp:Component)-[r:RELATED]->(k:Knowledge) WHERE cmp.nodeId = $componentId RETURN k")
    Set<KnowledgeNode> findRelatedKnowledge(Long componentId);
    
    /**
     * 根据知识点ID查找相关组件
     * @param knowledgeId 知识点ID
     * @return 相关组件列表
     */
    @Query("MATCH (cmp:Component)-[r:RELATED]->(k:Knowledge) WHERE k.nodeId = $knowledgeId RETURN cmp")
    Set<ComponentNode> findByKnowledgeId(Long knowledgeId);
    
    /**
     * 查询组件所属案例信息
     * @param componentId 组件ID
     * @return 所属案例节点
     */
    @Query("MATCH (c:Case)-[:CASE_COMPONENT]->(cmp:Component) WHERE cmp.nodeId = $componentId RETURN c")
    CaseNode findCaseByComponentId(Long componentId);
    
    /**
     * 查询组件的ID及其所属案例ID
     * @param componentId 组件ID
     * @return 案例ID
     */
    @Query("MATCH (c:Case)-[:CASE_COMPONENT]->(cmp:Component) WHERE cmp.nodeId = $componentId RETURN c.nodeId")
    Long findCaseIdByComponentId(Long componentId);
} 